<template>
  <div class="app-container">
    <!-- 统计模块 -->
    <el-table ref="table" :data="crud.data" size="small">
      <el-table-column type="index" width="150rpx" label="客户编号" />
      <el-table-column prop="name" label="客户名称" />
      <el-table-column prop="sumpric" label="客户总消费" />
    </el-table>
    <div>
      <div id="chart" style="width: 38rem; height: 38rem;display:inline-block;" />
      <div id="main1" style="width: 38rem; height: 38rem;display:inline-block;" />
      <div id="main2" style="width: 30rem; height: 30rem;display:inline-block;" />
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
const defaultForm = {
  sumpric: null,
  name: null
}
export default {
  name: 'Statistics1',
  components: {},
  mixins: [presenter(), header(), form(defaultForm), crud()],

  cruds() {
    return CRUD({ title: '客户贡献', url: '/api/cstCustomer/gongxian', idField: 'name', sort: 'sumpric,desc' })
  },
  data() {
    return {
      list: []
    }
  },
  mounted() {
    setTimeout(() => {
      const data1 = []
      const data2 = []
      this.list.forEach(e => {
        data1.push(e.name)
        data2.push(e.sumpric)
      })
      const data3 = this.list.map((item, index) => {
        return Object.assign({}, { 'name': item.name, 'value': item.sumpric })
      })
      console.log(data1)
      this.chart = echarts.init(document.getElementById('chart'))
      const chartData = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: data1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data2,
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            }
          }
        ]
      }
      this.chart.setOption(chartData)
      this.main1 = echarts.init(document.getElementById('main1'))
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: data1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data2,
            type: 'line'
          }
        ]
      }
      option && this.main1.setOption(option)

      this.main2 = echarts.init(document.getElementById('main2'))
      const option1 = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '客户消费占比',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: data3
          }
        ]
      }
      option1 && this.main2.setOption(option1)
    }, 1000)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterRefresh]() {
      this.list = this.crud.data
    }
  }
}
</script>
<style scoped>
</style>
